package com.wigem.fa.service;

import com.wigem.fa.entity.FaEntInfoEntity;
import com.wigem.fa.entity.RegisterEntity;
import com.wigem.fa.mapper.FaEntInfoMapper;
import com.wigem.fa.vo.FaEntInfoVo;
import com.wigem.framework.common.pojo.PageResult;
import com.wigem.framework.common.util.common.GuidUtils;
import com.wigem.framework.mybatis.core.query.LambdaQueryWrapperX;
import com.wigem.system.entity.SystemUserEntity;
import com.wigem.system.entity.SystemUserRelationEntity;
import com.wigem.system.mapper.SystemUserMapper;
import com.wigem.system.service.SystemUserService;
import com.wigem.system.vo.SystemUserVo;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.Date;
import java.util.List;

/**
 * 申报企业基本信息Service业务层处理
 *
 * @author DH
 * @date 2024-10-20
 */
@Service
public class RegisterServiceImpl implements RegisterService {

    @Resource
    private FaEntInfoMapper faEntInfoMapper;

    @Resource
    private SystemUserMapper systemUserMapper;
    @Resource
    private SystemUserService systemUserService;


    @Override
    public int register(RegisterEntity registerEntity) {
        //创建之前先验证账号是否存在
        SystemUserEntity userEntity = systemUserMapper.selectOne(new LambdaQueryWrapperX<SystemUserEntity>().eq(SystemUserEntity::getUserName, registerEntity.getUserName()));
        if (ObjectUtils.isEmpty(userEntity)) {
            // 新增用户信息
            SystemUserVo systemUserVo = new SystemUserVo();
            systemUserVo.setUserName(registerEntity.getUserName());
            systemUserVo.setRealName(registerEntity.getRealName());
            systemUserVo.setPassword(registerEntity.getPassword());
            systemUserVo.setUpdateTime(LocalDateTime.now());
            systemUserVo.setUserCode("123456");
            systemUserVo.setSecurityCode(123456L);
            //新增角色信息
            SystemUserRelationEntity systemUserRelationEntity = new SystemUserRelationEntity();
            systemUserRelationEntity.setControlType("role");
            systemUserRelationEntity.setCreateTime(LocalDate.now());
            systemUserRelationEntity.setCreateBy("注册");
            //企业角色
            systemUserRelationEntity.setObjectId("1313907211c74fb4b6bd7c6cede72aac");
            systemUserVo.setRoleData(systemUserRelationEntity);
            systemUserService.save(systemUserVo);
            SystemUserEntity user = systemUserMapper.selectOne(new LambdaQueryWrapperX<SystemUserEntity>().eq(SystemUserEntity::getUserName, registerEntity.getUserName()));
            //用户账号必须新增成功才能新增企业信息
            if (!ObjectUtils.isEmpty(user)) {
                FaEntInfoVo vo = new FaEntInfoVo();
                vo.setSocialCreditCode(registerEntity.getUserName());
                vo.setOrgName(registerEntity.getOrgName());
                vo.setLinkMan(registerEntity.getLinkMan());
                vo.setLinkManPhone(registerEntity.getLinkManPhone());
                vo.setEnableMark("1");//默认启用
                vo.setLinkManEmail(registerEntity.getLinkManEmail());
                vo.setCreateTime(new Date());
                vo.setCreateUser(user.getUserId());
                FaEntInfoEntity faEntInfoEntity = new FaEntInfoEntity();
                BeanUtils.copyProperties(vo, faEntInfoEntity);
                faEntInfoEntity.create();
                //创建之前先验证企业是否存在
                faEntInfoMapper.insert(faEntInfoEntity);
            }
            return 0;
        } else {
            return 1;
        }
    }
}
